home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / ioctl.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  5KB  |  211 lines

  1. /*
  2.  * ioctl() emulation for MiNT; written by Eric R. Smith and placed
  3.  * in the public domain
  4.  */
  5.  
  6. #include <errno.h>
  7. #include <mintbind.h>
  8. #include <ioctl.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <linea.h>    /* for TIOCGWINSZ under TOS */
  12. #include <support.h>
  13. #include <stat.h>
  14. #include "lib.h"    /* for __open_stat */
  15.  
  16. extern int __mint;    /* MiNT version */
  17. int _ttydisc = NTTYDISC;
  18. int _ldisc = LLITOUT;
  19.  
  20. /* in read.c */
  21. extern struct tchars __tchars;
  22. extern struct ltchars __ltchars;
  23.  
  24. int ioctl(fd, cmd, arg)
  25.     int fd, cmd;
  26.     void *arg;
  27. {
  28.     long r;
  29.     int istty = isatty(fd);
  30.     struct sgttyb *sg = (struct sgttyb *) arg;
  31.     int null_fd;
  32.     long baud;
  33.  
  34.     if (istty) {
  35.         switch (cmd) {
  36.         case TIOCGETD:
  37.             *((int *)arg) = _ttydisc;
  38.             return 0;
  39.             break;
  40.         case TIOCSETD:
  41.             _ttydisc = *((int *)arg);
  42.             return 0;
  43.             break;
  44.         case TIOCLGET:
  45.             *((int *)arg) = _ldisc;
  46.             return 0;
  47.             break;
  48.         case TIOCLSET:
  49.             _ldisc = *((int *)arg);
  50.             return 0;
  51.             break;
  52.         case TIOCSWINSZ:
  53.             if (__mint < 9)
  54.                 return 0;
  55.             break;
  56.         case TIOCGWINSZ:
  57.             if (__mint < 9) {
  58.                 struct winsize *win = (struct winsize *)arg;
  59. #ifndef __SOZOBON__
  60.                 (void)linea0();
  61. #else /* __SOZOBON__ */
  62.                 linea0();
  63. #endif /* __SOZOBON__ */
  64.                 win->ws_row = V_CEL_MY + 1;
  65.                 win->ws_col = V_CEL_MX + 1;
  66.                 win->ws_xpixel = V_X_MAX;
  67.                 win->ws_ypixel = V_Y_MAX;
  68.                 return 0;
  69.             }
  70.             break;
  71. #ifdef __MINT__
  72.         case TIOCNOTTY:
  73.             if (__mint) {
  74.                 if ((fd < 0) || !(_isctty(fd))) {
  75.                     errno = EBADF;
  76.                     return -1;
  77.                 }
  78.                 (void) Fclose(fd);
  79.                 null_fd = (int) Fopen(__mint < 9 ? "V:\\null"
  80.                         : "U:\\dev\\null", O_RDWR);
  81.                 (void) Fforce(-1, null_fd);
  82.                 __open_stat[__OPEN_INDEX(-1)].status =
  83.                     FH_UNKNOWN;
  84.                 __open_stat[__OPEN_INDEX(fd)].status =
  85.                     FH_UNKNOWN;
  86.                 if (null_fd != fd) {
  87.                     (void) Fforce(fd, null_fd);
  88.                     (void) Fclose(null_fd);
  89.                 }
  90.                 return 0;
  91.             }
  92.             break;
  93. #endif /* __MINT__ */
  94.         default:
  95.             break;
  96.         }
  97.     }
  98.  
  99.     if (__mint) {
  100.       switch (cmd) {
  101.         case TIOCCDTR:
  102.           baud = 0;
  103.           r = Fcntl(fd, &baud, TIOCOBAUD);
  104.           if (r < 0) {
  105.             errno = (int) -r;
  106.         return -1;
  107.           }
  108.           return 0;
  109.           break;
  110.         case TIOCSDTR:
  111.           baud = -1;
  112.           r = Fcntl(fd, &baud, TIOCOBAUD);
  113.           if (r < 0) {
  114.             errno = (int) -r;
  115.         return -1;
  116.           }
  117.           r = Fcntl(fd, &baud, TIOCOBAUD);
  118.           if (r < 0) {
  119.             errno = (int) -r;
  120.         return -1;
  121.           }
  122.           return 0;
  123.           break;
  124.         case TIOCMGET:
  125.           if (__mint >= 0x10a) {
  126.                 char g;
  127.                 long ssp;
  128.                 short *mfp;
  129.                 short m;
  130.                 struct stat sb;
  131.                 long *msig;
  132.  
  133.                 msig = (long *) arg;
  134.                 r = Fcntl(fd, (long)&sb, FSTAT);
  135.                 if (r < 0) {
  136.           errno = -r;
  137.                   return -1;
  138.         }
  139.                 if (((sb.st_mode & S_IFMT) == S_IFCHR) && (sb.st_rdev == 257))
  140.                 {
  141.                   *msig = TIOCM_DSR;
  142.                   g = Giaccess(0, 14);
  143.                   *msig |= ((g & (1 << 3)) ? 0 : TIOCM_RTS);
  144.                   *msig |= ((g & (1 << 4)) ? 0 : TIOCM_DTR);
  145.                   mfp = ((short *) 0xfffffa00L);
  146.                   ssp = Super(0L);
  147.                   m = *mfp & 0xff;
  148.                   Super(ssp);
  149.                   *msig |= ((m & (1 << 1)) ? 0 : TIOCM_CAR);
  150.                   *msig |= ((m & (1 << 2)) ? 0 : TIOCM_CTS);
  151.                   *msig |= ((m & (1 << 6)) ? 0 : TIOCM_RNG);
  152.                   return 0;
  153.                 }
  154.                 errno = EINVAL;
  155.                 return -1;
  156.                 break;
  157.           }
  158.         default:
  159.           r = Fcntl(fd, arg, cmd);
  160.           break;
  161.         }
  162.     }
  163.     else if (istty) {
  164.         r = 0;
  165.         switch(cmd) {
  166.         case TIOCSETP:
  167.             fd = __OPEN_INDEX(fd);
  168.             if (fd < 0 || fd >= __NHANDLES)
  169.                 fd = __NHANDLES - 1;
  170.             __open_stat[fd].flags = sg->sg_flags;
  171.             break;
  172.         case TIOCGETP:
  173.             fd = __OPEN_INDEX(fd);
  174.             if (fd < 0 || fd >= __NHANDLES)
  175.                 fd = __NHANDLES - 1;
  176.             sg->sg_flags = __open_stat[fd].flags;
  177.             sg->sg_ispeed = sg->sg_ospeed = 0;
  178.             sg->sg_erase = 'H' & 0x1f;
  179.             sg->sg_kill = 'U' & 0x1f;
  180.             break;
  181.         case TIOCGETC:
  182.             *((struct tchars *)arg) = __tchars;
  183.             break;
  184.         case TIOCSETC:
  185.             __tchars = *((struct tchars *)arg);
  186.             break;
  187.         case TIOCGLTC:
  188.             *((struct ltchars *)arg) = __ltchars;
  189.             break;
  190.         case TIOCSLTC:
  191.             __ltchars = *((struct ltchars *)arg);
  192.             break;
  193.         case TIOCGPGRP:
  194.             *((long *)arg) = 0;
  195.             break;
  196.         case TIOCSPGRP:
  197.             break;
  198.         default:
  199.             r = -EINVAL;
  200.         }
  201.     }
  202.     else
  203.         r = -EINVAL;
  204.  
  205.     if (r < 0) {
  206.         errno = (int) -r;
  207.         return -1;
  208.     }
  209.     return (int) r;
  210. }
  211.